package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.BmsConfig;
import com.tbit.main.pojo.BrandRemoteConfig;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.BrandRemoteConfigService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * bms配置
 *
 * @author LMY
 * @create 2022-07-15 14:33
 */
@RestController
@RequestMapping("/bmsConfig")
public class BmsConfigController {

    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountService accountService;

    /**
     * 获取bms配置
     *
     * @param token
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult<BmsConfig> getByAccountId(@NotNull String token) {

        AccountUser accountUser = tokenService.getAccount(token);

        BrandRemoteConfig brandRemoteConfig = brandRemoteConfigService.getByAccountIdAndType(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId(), BrandRemoteConfigConstant.BMS);

        BmsConfig bmsConfigByStr = new BmsConfig();
        if (brandRemoteConfig != null) {
            //转换为对象
            bmsConfigByStr = new BmsConfig().getBmsConfigByStr(brandRemoteConfig.getParamKV());
        }

        return JsonResult.succ(bmsConfigByStr);
    }

    /**
     * 修改bms配置
     *
     * @param token
     * @param bmsConfig
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.BMSCONFIG, methods = MethodEnum.BMSCONFIG_UPDATE)
    public JsonResult update(@NotNull String token, @NotNull BmsConfig bmsConfig) {

        AccountUser accountUser = tokenService.getAccount(token);

        bmsConfig.param();

        Integer accountId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();

        BrandRemoteConfig brandRemoteConfigDB = brandRemoteConfigService.getByAccountIdAndType(accountId, BrandRemoteConfigConstant.BMS);

        if (brandRemoteConfigDB != null) {
            brandRemoteConfigService.del(accountId, BrandRemoteConfigConstant.BMS);
        }

        //转换为key=value形式
        String bmsConfigStr = bmsConfig.toBmsConfigStr();
        BrandRemoteConfig brandRemoteConfig = new BrandRemoteConfig();
        brandRemoteConfig.setAccountId(accountId);
        brandRemoteConfig.setConfigType(BrandRemoteConfigConstant.BMS);
        brandRemoteConfig.setParamKV(bmsConfigStr);
        brandRemoteConfig.setUpdateTime(DateTimeUtil.getNowTime());
        brandRemoteConfigService.insert(brandRemoteConfig);
        return JsonResult.succ();
    }

}
